package com.lizard.schedule.manager.msc;

import android.content.Context;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.lizard.schedule.ScheduleApplication;
import com.lizard.schedule.consts.FilePathConst;
import com.lizard.schedule.log.ScheduleLog;

import java.io.File;

/**
 * 科大讯飞语音听写管理类
 */
public class SpeechRecognizerManager {

    public static final String TAG = SpeechRecognizerManager.class.getSimpleName();
    private static SpeechRecognizerManager instance;
    /**
     * 开放平台appid
     */
    private final String APPID = "55fa7962";

    private SpeechRecognizerManager(){}

    public static SpeechRecognizerManager getInstance(){
        if(instance == null){
            synchronized (SpeechRecognizerManager.class){
                if(instance == null){
                    instance = new SpeechRecognizerManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     */
    public void init(){
        SpeechUtility.createUtility(ScheduleApplication.applicationContext, SpeechConstant.APPID + "=" + APPID);
    }

    /**
     * 构建SDK自带语音输入UI控件Dialog
     *
     * @param context
     * @param recognizerDialogListener
     * @return
     */
    public RecognizerDialog buildRecognizerDialog(Context context, RecognizerDialogListener recognizerDialogListener){
        RecognizerDialog dialog = new RecognizerDialog(context, new InitListener() {
            @Override
            public void onInit(int code) {
                ScheduleLog.v(TAG, "SpeechRecognizer init() code = " + code);
                if (code != ErrorCode.SUCCESS) {
                    ScheduleLog.v(TAG, "初始化失败，错误码：" + code);
                }
            }
        });
        // 清空参数
        dialog.setParameter(SpeechConstant.PARAMS, null);
        // 设置听写引擎为云端(其他选择有本地、混合)
        dialog.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        // 设置语言
        dialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        // 设置语言区域
        dialog.setParameter(SpeechConstant.ACCENT, "mandarin");
        // 设置返回结果格式
        dialog.setParameter(SpeechConstant.RESULT_TYPE, "json");
        // 设置语音前端点:静音超时时间，即用户多长时间不说话则当做超时处理
        dialog.setParameter(SpeechConstant.VAD_BOS, "4000");
        // 设置语音后端点:后端点静音检测时间，即用户停止说话多长时间内即认为不再输入， 自动停止录音
        dialog.setParameter(SpeechConstant.VAD_EOS, "1000");
        // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
        dialog.setParameter(SpeechConstant.ASR_PTT, "0");
        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        dialog.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        dialog.setParameter(SpeechConstant.ASR_AUDIO_PATH, new File(FilePathConst.SPEECH_RECOGNIZER_AUDIO_DIR, "iat").getAbsolutePath());
        dialog.setListener(recognizerDialogListener);

        return dialog;
    }

    /**
     * 构建语音识别对象
     *
     * @param context
     * @return
     */
    public SpeechRecognizer buildSpeechRecognizer(Context context){
        SpeechRecognizer speechRecognizer = SpeechRecognizer.createRecognizer(context, new InitListener() {

            @Override
            public void onInit(int code) {
                ScheduleLog.v(TAG, "SpeechRecognizer init() code = " + code);
                if (code != ErrorCode.SUCCESS) {
                    ScheduleLog.v(TAG, "初始化失败，错误码：" + code);
                }
            }
        });
        // 清空参数
        speechRecognizer.setParameter(SpeechConstant.PARAMS, null);
        // 设置听写引擎为云端(其他选择有本地、混合)
        speechRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        // 设置语言
        speechRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        // 设置语言区域
        speechRecognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
        // 设置返回结果格式
        speechRecognizer.setParameter(SpeechConstant.RESULT_TYPE, "json");
        // 设置语音前端点:静音超时时间，即用户多长时间不说话则当做超时处理
        speechRecognizer.setParameter(SpeechConstant.VAD_BOS, "4000");
        // 设置语音后端点:后端点静音检测时间，即用户停止说话多长时间内即认为不再输入， 自动停止录音
        speechRecognizer.setParameter(SpeechConstant.VAD_EOS, "1000");
        // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
        speechRecognizer.setParameter(SpeechConstant.ASR_PTT, "0");
        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        speechRecognizer.setParameter(SpeechConstant.AUDIO_FORMAT,"wav");
        speechRecognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH, new File(FilePathConst.SPEECH_RECOGNIZER_AUDIO_DIR, "iat").getAbsolutePath());
        return speechRecognizer;
    }

}
